package szhn.controller;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;
import szhn.model.BookModel;
import szhn.model.ResultModel;
import szhn.model.UserModel;
import szhn.model.UserOperationModel;
import szhn.service.BookService;
import szhn.service.SystemService;

/**
 * 图书管理类
 * 
 * @author Panxinxing 2020年5月12日
 *
 */
@RestController
@Slf4j
@RequestMapping("/book")
public class BookController {

	@Autowired
	private BookService bookService;

	@Autowired
	private SystemService systemService;

	/**
	 * 查询图书
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ResponseEntity<ResultModel> query(HttpServletRequest request,
			@RequestParam(value = "bookName", required = false) String bookName,
			@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
			@RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
		UserModel user = (UserModel) request.getSession().getAttribute("user");
		log.info("BookController查询图书记录username={},bookName={},pageNum={},pageSize={}", user.getUsername(), bookName,
				pageNum, pageSize);

		ResultModel result = new ResultModel();
		//想了很久，在redis中无法实现带搜索条件的增删查改，如果想将增删查改功能都放置入缓存，最好是用ES
		//当查询条件不存在时，查redis缓存；当查询条件存在时，查询库
		if(StringUtils.isBlank(bookName)) {
			PageInfo<Object> pageInfo = bookService.query(pageNum, pageSize);
			result.setData(pageInfo);
		}else {
			PageHelper.startPage(pageNum, pageSize);
			Page<BookModel> list = bookService.query(bookName);
			PageInfo<BookModel> pageInfo = new PageInfo<BookModel>(list);
			result.setData(pageInfo);
		}

		result.setStatus(1);
		result.setMsg("查询成功");
		return ResponseEntity.ok(result);
	}

	/**
	 * 新增图书
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/", method = RequestMethod.POST)
	public ResponseEntity<ResultModel> save(HttpServletRequest request, @RequestBody BookModel bookModel) {
		UserModel user = (UserModel) request.getSession().getAttribute("user");
		String userName = user.getUsername();
		log.info("LibraryController新增图书username={},bookModel={}", userName, JSON.toJSONString(bookModel));
		ResultModel result = new ResultModel();

		// 用户对象
		bookModel.setId(UUID.randomUUID().toString());
		bookModel.setCreateUser(userName);
		bookModel.setLastUpdateUser(userName);

		boolean flag = bookService.save(bookModel);

		// 用户操作日志
		UserOperationModel userOperationModel = new UserOperationModel();
		userOperationModel.setId(UUID.randomUUID().toString());
		userOperationModel.setUsername(userName);
		userOperationModel.setCreateUser(userName);
		userOperationModel.setOperation("addBook");
		userOperationModel.setOperationDetail(JSON.toJSONString(bookModel));
		systemService.userOperation(userOperationModel);

		result.setStatus(1);
		result.setData(flag);
		return ResponseEntity.ok(result);
	}

	/**
	 * 修改图书信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	public ResponseEntity<ResultModel> modify(HttpServletRequest request, @RequestBody BookModel bookModel) {
		UserModel user = (UserModel) request.getSession().getAttribute("user");
		String userName = user.getUsername();
		log.info("BookController修改图书username={},bookModel={}", userName, JSON.toJSONString(bookModel));
		ResultModel result = new ResultModel();

		// 判断
		if (StringUtils.isEmpty(bookModel.getId())) {
			result.setStatus(0);
			result.setMsg("id为空");
			return ResponseEntity.ok(result);
		}

		// 用户对象
		bookModel.setCreateUser(userName);
		bookModel.setLastUpdateUser(userName);

		boolean flag = bookService.modify(bookModel);

		// 用户操作日志
		UserOperationModel userOperationModel = new UserOperationModel();
		userOperationModel.setId(UUID.randomUUID().toString());
		userOperationModel.setUsername(userName);
		userOperationModel.setCreateUser(userName);
		userOperationModel.setOperation("modifyBook");
		userOperationModel.setOperationDetail(JSON.toJSONString(bookModel));
		systemService.userOperation(userOperationModel);

		result.setStatus(1);
		result.setData(flag);
		return ResponseEntity.ok(result);
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<ResultModel> delete(HttpServletRequest request, @PathVariable(value = "id") String id) {
		UserModel user = (UserModel) request.getSession().getAttribute("user");
		String userName = user.getUsername();
		log.info("BookController删除username={},id={}", userName, id);

		boolean flag = bookService.delete(id, userName);

		// 用户操作日志
		UserOperationModel userOperationModel = new UserOperationModel();
		userOperationModel.setId(UUID.randomUUID().toString());
		userOperationModel.setUsername(user.getUsername());
		userOperationModel.setCreateUser(user.getUsername());
		userOperationModel.setOperation("deleteBook");
		userOperationModel.setOperationDetail(id);

		ResultModel result = new ResultModel();
		result.setStatus(1);
		result.setData(flag);
		return ResponseEntity.ok(result);
	}
}
